package com.project.course.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.project.course.entity.User;
import com.project.course.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: zhangqingbiao
 * @Date: 2021/3/30 20:11
 */
@RestController
@Slf4j
@Api(tags = "用户管理服务")
@RequestMapping(("/user"))
public class UserController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Resource
    private UserService userService;

    @ApiOperation(value = "用户信息使用jdbcTemplate", notes = "用户信息")
    @ApiImplicitParams({
            // 注意加参数： Paramtype=path 这里的path类型，而不是query类型，query类型是查询的？a=xxx&b=xxx这种的类型
            @ApiImplicitParam(name = "name", value = "用户姓名", dataType = "string", paramType = "path", required = true),
            @ApiImplicitParam(name = "duty", value = "用户职称", dataType = "string", paramType = "path", required = false),
            @ApiImplicitParam(name = "address", value = "地址", dataType = "string", paramType = "path", required = false)
    })
//    @GetMapping(value = "/getUserInfo/{name}/{duty}/{address}")
    @RequestMapping(value = "/getUserInfo/{name}/{duty}/{address}", method = RequestMethod.GET)
    public String getUserInfo(@PathVariable("name") String name, @PathVariable("duty") String duty, @PathVariable("address") String address) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select *  from sys_user ")
                .append("where ")
                .append("name like ").append("'%").append(name.trim()).append("%'").append(" and ")
                .append("duty like ").append("'%").append(duty.trim()).append("%'").append(" and ")
                .append("address like ").append("'%").append(address.trim()).append("%'")
                .append(";");
        System.out.println(stringBuilder.toString());

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(stringBuilder.toString());
        List<User> users = JSONArray.parseArray(JSONObject.toJSONString(maps), User.class);
//        String userList = JSONObject.toJSONString(maps);
        return users.toString();
    }

    @ApiOperation(value = "用户信息使用mybatis", notes = "用户信息")
    //    @GetMapping(value = "/getUserInfo/{name}/{duty}/{address}")
    @RequestMapping(value = "/selectUserByCondition", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户姓名", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "address", value = "地址", dataType = "string", paramType = "query", required = false),
            @ApiImplicitParam(name = "description", value = "描述", dataType = "string", paramType = "query", required = false)
    })
    public String selectUserByCondition(String name, String address, String description) {
        System.out.println("name:" + name + ",address=" + address + ",description:" + description);
        List<User> users = userService.selectUserByCondition(name.trim(), address.trim(), description.trim());
        return users.toString();
    }
}
